/**
 *  Copyright 2012 Rafal Korytkowski
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.googlecode.rapidjunit;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * Controls the plug-in life cycle.
 * 
 * @author Rafal Korytkowski
 */
public class RapidJUnit extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.googlecode.rapidjunit";

	// The shared instance
	private static RapidJUnit plugin;

	private static RapidJUnitManager manager;

	/**
	 * The constructor
	 */
	public RapidJUnit() {
	}

	/**
	 * @see AbstractUIPlugin#start(BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		manager = new RapidJUnitManager();
		manager.load();
	}

	/**
	 * @see AbstractUIPlugin#stop(BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		manager.save();
		manager = null;
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static RapidJUnit getDefault() {
		return plugin;
	}

	/**
	 * Returns an image descriptor for the image file at the given plug-in
	 * relative path
	 * 
	 * @param path
	 *            the path
	 * @return the image descriptor
	 */
	public static ImageDescriptor getImageDescriptor(String path) {
		return imageDescriptorFromPlugin(PLUGIN_ID, path);
	}

	/**
	 * @see AbstractUIPlugin#getLog()
	 * @param exception
	 */
	public static void log(Throwable exception) {
		getDefault().getLog().log(
				new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "Error",
						exception));
	}

	/**
	 * Logs the exception and optionally displays a message dialog.
	 * 
	 * @param exception
	 * @param notify
	 */
	public static void log(final Throwable exception, boolean notify) {
		if (notify) {
			Display.getDefault().asyncExec(new Runnable() {
				public void run() {
					IWorkbench workbench = plugin.getWorkbench();
					IWorkbenchWindow window = (workbench != null) ? workbench
							.getActiveWorkbenchWindow() : null;
					Shell shell = (window != null) ? window.getShell() : null;
					MessageDialog.openError(shell, "Error",
							exception.getMessage());
				}
			});
		}
		log(exception);
	}

	/**
	 * @see AbstractUIPlugin#getStateLocation()
	 */
	public static IPath getLocation() {
		return getDefault().getStateLocation();
	}

	/**
	 * @return manager
	 */
	public static RapidJUnitManager getManager() {
		return manager;
	}
}
